Polymorphism: Using Interfaces and Inheritance

Lab objective: Explore Java interfaces, inheritance, polymorphism and the development of generic code

Consider the method with the signature public static boolean find(int [] data, int number), that searches an integer array named data for an occurrence of the integer number. The method public static boolean find(float [] data, float number) performs the same function for an array of floats. Again, the method public static boolean find(double [] data, double number) performs the same function for an array of doubles.

Note the redundancy in the above methods. All of these methods use the same code, with different data types.

  1. How would you write a generic search routine that can be used to search an array of any kind of data, for an occurence of an object of the same type. That is, one method that can be used to search all arrays regardless of the type of the array. Demonstrate your approach by adding a generic find method to the following Java program, to search an array of Integers, Floats, Doubles, and an array of Pumpkins. The class Pumpkin is defined here. Two pumpkins are considered equal if they have the same weight. What changes would you have to make to the Pumpkin class to enable this?.
  2. Now consider the method public static int countGreater(int [] data, int number), that determines how many of the elements in data have a value strictly greater than the integer number. How would you write a generic version of this method? That is, one method that can be used with all data types, not just integers. Demonstrate your approach by adding a generic countGreater method to the following Java program, to search an array of Integers, Floats, Doubles, and an array of Pumpkins. One pumpkin is considered greater than another one if it weighs more. What changes would you have to make to the Pumpkin class to enable this?

Note: Your code must conform to the following coding conventions,